﻿package artin.A3D7.main {
	
	import alternativa.engine3d.containers.DistanceSortContainer;
	import alternativa.engine3d.core.Object3DContainer;
	import alternativa.engine3d.controllers.SimpleObjectController;
	import alternativa.engine3d.core.MipMapping;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.engine3d.loaders.MaterialLoader;
	import alternativa.engine3d.primitives.Box;
	
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.display.GradientType;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.system.LoaderContext;
	import flash.net.URLRequest;
	import flash.geom.Matrix;
	import artin.A3D7.template.AlternativaTemplate;
	/**
	 * 材质 例子
	 */
	[SWF(backgroundColor="#000000", frameRate="100", width="800", height="600")]
	public class TextureMaterialExample extends Sprite {
		
		/*// 绑定外部图片（官方demo使用的方法）
		[Embed(source="images/texture.jpg")]
		static private const EmbedTexture:Class;*/
		
		// 模板
		private var scene:AlternativaTemplate;
		// 立方体容器
		private var boxContainer:Object3DContainer;
		// 立方体容器控制器
		private var boxContainerController:SimpleObjectController;
		
		/**
		 * 构造函数
		 */
		public function TextureMaterialExample() {
			
			/**
			 * 创建模板
			 */
			scene = new AlternativaTemplate(this);
			// 调整摄像头
			scene.cameraController.setObjectPosXYZ(0,-1500,0);
			scene.cameraController.lookAtXYZ(0, 0, 0);
			addChild(scene.camera.diagram);
			// 模板类的摄像头控制器解除了所有绑定
			// 可通过设置默认绑定，重新启用键盘控制功能
			scene.cameraController.setDefaultBindings();
			scene.cameraController.speed = 400; // 移动速度
			
			/**
			 * 立方体容器
			 */
			boxContainer = new DistanceSortContainer();
			scene.container.addChild(boxContainer);
			
			
			/**
			 * 创建填充材质
			 */
			// 无边框
			var fillMaterial1 = new FillMaterial(0x666666);
			
			// 有边框
			var fillMaterial2 = new FillMaterial(0x666666, 1, 0, 0xFF0000);
			 
			// 半透明
			var fillMaterial3 = new FillMaterial(0x666666, 0.8);
			 
			// 填充材质数组
			var fillMaterialAry:Array = [];
			fillMaterialAry.push(new FillMaterial(0x6666FF));
			fillMaterialAry.push(new FillMaterial(0x66FF66));
			fillMaterialAry.push(new FillMaterial(0xFF6666));
			fillMaterialAry.push(new FillMaterial(0x66FFFF));
			fillMaterialAry.push(new FillMaterial(0xFF66FF));
			fillMaterialAry.push(new FillMaterial(0xFFFF66));
			
			
			/**
			 * 创建贴图材质
			 */
			// 方法1：使用绑定的外部图片（官方demo使用的方法）
			// MipMapping.NONE：常规解析
			[Embed(source="bin-debug/resources/images/texture.jpg")] const EmbedTexture:Class;
			var bitmapData1:BitmapData = new EmbedTexture().bitmapData;
			var textureMaterial1:TextureMaterial = new TextureMaterial(bitmapData1, false, true, MipMapping.NONE);
			
			// 方法2：使用库中的图片
			// MipMapping.OBJECT_DISTANCE：按离摄像头的距离进行解析
			var bitmapData2:BitmapData = new Alternativa3d();
			var textureMaterial2:TextureMaterial = new TextureMaterial(bitmapData2, false, true, MipMapping.OBJECT_DISTANCE);
			
			// 方法3：使用Loader类 加载外部图片
			var bitmapData3:BitmapData;
			var textureMaterial3:TextureMaterial = new TextureMaterial();
			var imgLoader:Loader = new Loader();
			imgLoader.load(new URLRequest("resources/images/9ria.jpg"));
			imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoadComplete);
			// 需要加载完成才能获取位图源
			function imgLoadComplete(e:Event):void {
				bitmapData3 = e.target.content.bitmapData;
				textureMaterial3.texture = bitmapData3;
			}
			
			// 方法4：使用A3D内置的MaterialLoader类 加载外部图片（推荐）
			// MipMapping.PER_PIXEL：按离摄像头的像素进行解析
			var textureMaterial4:TextureMaterial = new TextureMaterial(null, false, true, MipMapping.PER_PIXEL);
			textureMaterial4.diffuseMapURL = "resources/images/ssnangua.jpg";
			var textures:Vector.<TextureMaterial> = new Vector.<TextureMaterial>;
			textures.push(textureMaterial4);
			var context:LoaderContext = new LoaderContext(true);
            var materialLoader:MaterialLoader = new MaterialLoader();
            materialLoader.load(textures,context);
			
			
			/**
			 * 
			 * 渐变材质
			 */
			// 获取渐变BitmapData
			var shape:Shape = new Shape();
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(100,100);
            shape.graphics.beginGradientFill(GradientType.LINEAR, [0xFF9900,0xFFFF00], [1,1], [0,255], matrix);
            shape.graphics.drawRect(0, 0, 100, 100);
            var gradientBitmpaData:BitmapData = new BitmapData(100, 100);
            gradientBitmpaData.draw(shape);
			// 材质
			var gradientFillMaterial:TextureMaterial = new TextureMaterial(gradientBitmpaData);
			
			
			/**
			 * 
			 * 创建立方体
			 */
			var box1:Box = new Box(300, 300, 300);
			box1.setMaterialToAllFaces(fillMaterial1);
			boxContainer.addChild(box1);
			box1.x = -500;
			box1.z = 500;
			
			var box2:Box = new Box(300, 300, 300, 3, 3, 3);
			box2.setMaterialToAllFaces(fillMaterial2);
			boxContainer.addChild(box2);
			box2.z = 500;
			
			var box3:Box = new Box(300, 300, 300);
			box3.setMaterialToAllFaces(fillMaterial3);
			boxContainer.addChild(box3);
			box3.x = 500;
			box3.z = 500;
			
			// 可以把材质作为参数传给Box
			// 也可以通过访问Box的faces属性，将材质赋值给各个面的material属性
			var box4:Box = new Box(300, 300, 300);
			box4.faces[0].material = fillMaterialAry[0];
			box4.faces[1].material = fillMaterialAry[1];
			box4.faces[2].material = fillMaterialAry[2];
			box4.faces[3].material = fillMaterialAry[3];
			box4.faces[4].material = fillMaterialAry[4];
			box4.faces[5].material = fillMaterialAry[5];
			boxContainer.addChild(box4);
			box4.x = -500;
			
			var box5:Box = new Box(300, 300, 300);
			box5.setMaterialToAllFaces(textureMaterial1);
			boxContainer.addChild(box5);
			
			var box6:Box = new Box(300, 300, 300);
			box6.setMaterialToAllFaces(textureMaterial2);
			boxContainer.addChild(box6);
			box6.x = 500;
			
			var box7:Box = new Box(300, 300, 300);
			box7.setMaterialToAllFaces(textureMaterial3);
			boxContainer.addChild(box7);
			box7.x = -500;
			box7.z = -500;
			
			var box8:Box = new Box(300, 300, 300);
			box8.setMaterialToAllFaces(textureMaterial4);
			//box8.setMaterialToAllFaces(textures[0]);//也可以
			boxContainer.addChild(box8);
			box8.z = -500;
			
			var box9:Box = new Box(300, 300, 300);
			box9.setMaterialToAllFaces(gradientFillMaterial);
			boxContainer.addChild(box9);
			box9.x = 500;
			box9.z = -500;
			
			
			/**
			 * 立方体容器控制器
			 */
			boxContainerController = new SimpleObjectController(stage, boxContainer, 100);
			boxContainerController.unbindAll();
			//在摄像头渲染之前执行
			scene.onPreRender = function():void {
				boxContainerController.update();
			};
			
			/**
			 * 调用模板类方法：开始渲染
			 */
			scene.startRendering();
		}
	}
}